﻿using System;
using System.Collections.Generic;

/* 
  *  软件日志定义
  *  作用：主要用来存放软件日志定义与规则
  *  创建时间：2019/07/20 wj
  *  修改时间：2019/08/12 wj
  */
namespace Common.Service.RecordLog
{
    public class Record
    {
        public Record() { }

        public int recordID = 0;
        // 日志数据：
        public RecordType type = RecordType.None;
        public string summary = String.Empty;
        public string message = String.Empty;

        public string tips = String.Empty;

        // 日志数据：
        public byte summaryData;

        /**
          * \brief 将Log数据转换为日志数据
          * 
          * \param[in] filePath Log数据
          */
        public void GetRecordData(string tmp)
        {
            // 关键词序号：
            int typeNum = tmp.IndexOf("Type");
            int messageNum = tmp.IndexOf("Message");
            int tipsNum = tmp.IndexOf("Tips");
            // 获取数据：
            string typeString = tmp.Substring(typeNum + 5, messageNum - typeNum - 7);
            ConvertStringToRecordType(typeString);
            message = tmp.Substring(messageNum + 8, tipsNum - messageNum - 10);
            tips = tmp.Substring(tipsNum + 5, tmp.Length - tipsNum - 5);
        }

        /**
      * \brief 将日志数据转换为Log数据
      */
        public String SetRecordData()
        {
            // 获取数据：
            return string.Format("Type:" + type + ", Message:" + message + ", Tips:" + tips);
        }

        /**
           * \brief 将Log数据转换为日志数据
           * 
           * \param[in] idString Log数据
           */
        public void GetRecordType(string idString)
        {
            bool valid = true;
            summaryData = Utility.Tool.StringToByte(idString, ref valid);
        }

        /**
           * \brief 日志数据分类
           * 
           * \param[in] filePath Log数据
           */
        public string ClassifyRecordData(string summaryString)
        {
            String classifyMessage = String.Empty;

            if (summaryString.StartsWith("0x") || summaryString.StartsWith("0X"))
            {
                summaryString = summaryString.Remove(0, 2);
            }
            string mainMark = summaryString.Substring(0, 2);
            string subMark = summaryString.Substring(2, 2);

            return classifyMessage;
        }

        /**
           * \brief 字符串转日志类型
           * 
           * \param[in] typeString 待转换字符串
           */
        public void ConvertStringToRecordType(string typeString)
        {
            switch (typeString)
            {
                case "info":
                    type = RecordType.Info;
                    break;
                case "warn":
                    type = RecordType.Warn;
                    break;
                case "error":
                    type = RecordType.Error;
                    break;
                default:
                    break;
            }
        }

        /**
           * \brief 日志类型转字符串
           * 
           * \revtal typeString 转换后字符串
           */
        public string ConvertRecordTypeToString()
        {
            string typeString = String.Empty;
            switch (type)
            {
                case RecordType.Info:
                    typeString = "info";
                    break;
                case RecordType.Warn:
                    typeString = "warn";
                    break;
                case RecordType.Error:
                    typeString = "error";
                    break;
                default:
                    break;
            }
            return typeString;
        }
    }

    public class Records
    {
        public Records() { }
        // 日志名称：
        public string recordsName = String.Empty;
        public string recordsPath = String.Empty;
        // 日志列表：
        public List<Record> recordList = new List<Record>();

        /**
           * \brief 消除重复错误 [用于解决重复报错问题]
           * 
           * \param[in] summary 消息标志符
           * \param[in] recordList 消息存放列表
           */
        public void RemoveRecordData()
        {
            if (0 != recordList.Count)
            {
                // 日志编号重排序
                for (int i = 0; i < recordList.Count; i++)
                {
                    if (String.IsNullOrWhiteSpace(recordList[i].message) &&  RecordType.Info == recordList[i].type)
                    {
                        recordList.Remove(recordList[i]);
                    }
                    else
                    {
                        recordList[i].recordID = i + 1;
                    }
                }
            }
        }

        /**
          * \brief 将TXT数据转换为日志数据
          * 
          * \param[in] filePath TXT数据
          */
        public void GetRecordsData(string name, string filePath)
        {
            recordsName = name;
            recordsPath = filePath;
        }
    }
}

// 词汇类型分布
public enum RecordType
{
    None,
    Info,      // 提示
    Warn,    // 警告
    Error,    // 错误
}